package com.yc.thread.deadLock;

/***
 * 死锁形成，当资源1被
 *
 */
public class DeadLockDemo {

    // 资源1
    private static Object resource1 = new Object();
    // 资源2
    private static Object resource2 = new Object();

    public static void main(String[] args) {

        new Thread(() -> {
           synchronized (resource1) {
               System.out.println(Thread.currentThread() + "get resource1");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread() + " waiting get resource2");
               synchronized (resource2) {
                   System.out.println(Thread.currentThread() + "get resource2");
               }
           }
        }, "Thread 1").start();

        new Thread(() -> {
            synchronized (resource2) {
                System.out.println(Thread.currentThread() + "get resource1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting get resource2");
                synchronized (resource1) {
                    System.out.println(Thread.currentThread() + "get resource2");
                }
            }
        }).start();

//       new Thread(() -> {
//           synchronized (resource2) {
//               System.out.println(Thread.currentThread() + "get resource2");
//               try {
//                   Thread.sleep(1000);
//               } catch (InterruptedException e) {
//                   e.printStackTrace();
//               }
//               System.out.println(Thread.currentThread() + "waiting get resource1");
//               synchronized (resource1) {
//                   System.out.println(Thread.currentThread() + "get resource1");
//               }
//           }
//       }, "Thread 2").start();
    }
}
